//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.DataLayer
{
    using System.Collections.Generic;
    using System;

    /// <summary>
    /// DataReferenceProvider
    /// </summary>
    public partial class DataReferenceProvider
    {
        #region CommonSearches
        /// <summary>
        /// Common Searches provided.
        /// </summary>
        public sealed class CommonSearches
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="CommonSearches"/> class.
            /// </summary>
            private CommonSearches()
            {
            }

            /// <summary>
            /// Finds the provider in registry.
            /// </summary>
            /// <param name="providerName">Name of the provider.</param>
            /// <param name="connection">The connection.</param>
            /// <returns></returns>
            public static DataReferenceProvider FindProviderInRegistry(string providerName, Connection connection)
            {
                if (string.IsNullOrEmpty(providerName))
                {
                    throw new ArgumentNullException("providerName");
                }

                if (null == connection)
                {
                    throw new ArgumentNullException("connection");
                }

                DataReferenceProvider regType = null;
                try
                {
                    DataReferenceProvider.ISearch searchQuery = DataReferenceProvider.ISearch.Create();
                    searchQuery.Query = DataReferenceProvider.ISearch.Name(StringField.Condition.Equals, providerName);

                    List<DataReferenceProvider> dataProviderList = DataReferenceProvider.Search(searchQuery, connection);
                    if (dataProviderList != null && dataProviderList.Count > 0)
                    {
                        regType = dataProviderList[0];
                    }
                }
                catch (RecordNotFoundException)
                {
                }

                return regType;
            }

            /// <summary>
            /// Gets the available providers.
            /// </summary>
            /// <param name="connection">The connection.</param>
            /// <returns></returns>
            public static IEnumerable<DataReferenceProvider> GetAvailableProviders(Connection connection)
            {
                if (null == connection)
                {
                    throw new ArgumentNullException("connection");
                }

                IEnumerable<DataReferenceProvider> providers = null;
                try
                {
                    providers = DataReferenceProvider.LoadAll(connection);
                }
                catch (RecordNotFoundException)
                {
                    throw;
                }
                return providers;
            }
        }
        #endregion
    }
}
